package com.jet.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jet.core.model.vo.PageVo;
import com.jet.core.enums.CommonStatus;
import com.jet.core.exception.CustomException;
import com.jet.merchant.domain.dto.MerchantDto;
import com.jet.merchant.domain.dto.RegisterDto;
import com.jet.merchant.domain.entity.ClerkAccount;
import com.jet.merchant.domain.entity.Merchant;
import com.jet.merchant.domain.query.MerchantQuery;
import com.jet.merchant.domain.vo.MerchantVo;
import com.jet.merchant.mapper.MerchantMapper;
import com.jet.merchant.remote.office.OfficeRemote;
import com.jet.merchant.remote.office.vo.MerchantIndustryVo;
import com.jet.merchant.service.IClerkAccountService;
import com.jet.merchant.service.IMerchantService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-02-14
 */
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements IMerchantService {

    private final IClerkAccountService clerkAccountService;
    private final OfficeRemote officeRemote;

    public MerchantServiceImpl(IClerkAccountService clerkAccountService, OfficeRemote officeRemote) {
        this.clerkAccountService = clerkAccountService;
        this.officeRemote = officeRemote;
    }

    @Override
    public void register(long merchantNo, long storeNo, String accountId, RegisterDto dto) {
        Merchant entity = new Merchant();
        entity.setMerchantNo(merchantNo);
        entity.setMerchantName("我的旺铺");
        entity.setAccountId(accountId);
        entity.setStatus(CommonStatus.ENABLED.getCode());
        entity.setSource(dto.getSource());
        super.save(entity);

        // 给一个试用版本
        officeRemote.industryTrial(merchantNo, storeNo, accountId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(long merchantNo, MerchantDto dto) {
        Merchant merchant = findByMerchantNo(merchantNo);
        if (merchant == null) {
            throw new CustomException("商户信息错误");
        }
        merchant.setMerchantName(dto.getMerchantName());
        merchant.setPcLogo(dto.getPcLogo());
        merchant.setMobileLogo(dto.getMobileLogo());
        merchant.setUpdateTime(LocalDateTime.now());
        merchant.setMerchantNo(merchantNo);
        super.updateById(merchant);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editMerchantName(long merchantNo, String merchantName) {
        Merchant merchant = findByMerchantNo(merchantNo);
        if (merchant == null) {
            throw new CustomException("商户信息错误");
        }
        merchant.setMerchantName(merchantName);
        merchant.setUpdateTime(LocalDateTime.now());
        merchant.setMerchantNo(merchantNo);
        super.updateById(merchant);
    }

    @Override
    public Merchant findByMerchantNo(long merchantNo) {
        Wrapper<Merchant> queryWrapper = new LambdaQueryWrapper<Merchant>()
                .eq(Merchant::getMerchantNo, merchantNo);
        return super.getOne(queryWrapper);
    }

    @Override
    public boolean existsMerchantNo(long merchantNo) {
        Wrapper<Merchant> wrapper = new LambdaQueryWrapper<Merchant>()
                .eq(Merchant::getMerchantNo, merchantNo);
        return super.exists(wrapper);
    }

    @Override
    public MerchantVo merchantInfo(long merchantNo) {
        MerchantVo vo = new MerchantVo();
        Merchant merchant = findByMerchantNo(merchantNo);
        if (merchant == null) {
            return null;
        }
        BeanUtils.copyProperties(merchant, vo);
        ClerkAccount account = clerkAccountService.getById(vo.getAccountId());
        if (account != null) {
            vo.setAccountName(account.getName());
            vo.setAccountPhone(account.getPhone());
        }
        MerchantIndustryVo industryMerchant = officeRemote.getMerchantIndustry(merchantNo);
        if (industryMerchant != null) {
            vo.setIndustryId(industryMerchant.getIndustryId());
            vo.setIndustryName(industryMerchant.getIndustryName());
            vo.setExpireDate(industryMerchant.getExpireDate());
            vo.setIndustryType(industryMerchant.getType());
        }
        return vo;
    }

    @Override
    public PageVo<Merchant> page(MerchantQuery query) {
        Page<Merchant> tempQuery = new Page<>(query.getPageIndex(), query.getPageSize());
        Wrapper<Merchant> wrapper = new LambdaQueryWrapper<Merchant>()
                .orderByDesc(Merchant::getCreateTime);
        IPage<Merchant> page = super.page(tempQuery, wrapper);
        return PageVo.result(query.getPageIndex(), query.getPageSize(), (int) page.getTotal(), page.getRecords());
    }
}
